<template>
  <div class="good_of_has_buy_box">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" type="index" :index="tableIndex">
      </el-table-column>
      <el-table-column
        label="名称"
        prop="good.name"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="价格" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.good.sellPrice | priceFilter }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">买家未付款</div>
          <div v-else-if="scope.row.status == 1">买家已付款</div>
          <div v-else-if="scope.row.status == 2">买家已收货</div>
        </template>
      </el-table-column>
      <el-table-column label="购买时间" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.modifyTime | timeFilter }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            v-if="scope.row.status == 1"
            @click="confirmReceipt(scope.row)"
            >确认收货</el-button
          >
          <el-button
            type="danger"
            @click="deleteRecord(scope.$index, scope.row)"
            >删除订单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      background
      hide-on-single-page
      class="pagination"
      :page-size="size"
      :current-page="current"
      :total="total"
      @prev-click="getGoodRecord"
      @next-click="getGoodRecord"
      @current-change="getGoodRecord"
    >
    </el-pagination>
  </div>
</template>

<script>
import { Loading } from "element-ui";
import axios from "@/request";

export default {
  name: "BuyGood",
  data() {
    return {
      current: 1,
      size: 5,
      total: 100,
      tableData: [],
    };
  },
  mounted() {
    this.getGoodRecord(1);
  },
  methods: {
    tableIndex(index) {
      return (this.current - 1) * this.size + index + 1;
    },
    getGoodRecord(current) {
      let loading = Loading.service({
        text: "正在加载数据...",
        target: ".good_of_has_buy_box",
      });
      axios
        .get("/record/getRecord", {
          params: {
            current: current,
            size: this.size,
            userType: 2,
            userId: this.$store.state.userModule.user.user.id,
            itemType: 1,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.total = res.data.data.total;
            this.tableData = res.data.data.records;
          }
          loading.close();
        })
        .catch((e) => {
          loading.close();
        });
    },
    confirmReceipt(row) {
      let loading = Loading.service({
        text: "正在收货...",
        target: ".good_of_has_buy_box",
      });
      axios
        .get("/record/confirmReceipt", {
          params: {
            id: row.id,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success(res.data.data);
          } else {
            this.$message.error(res.data.data);
          }
          this.getGoodRecord(this.current);
          loading.close();
        })
        .catch((e) => {
          loading.close();
        });
    },
    deleteRecord(index, row) {
      let loading = Loading.service({
        text: "正在删除数据...",
        target: ".good_of_has_buy_box",
      });
      axios
        .get("/record/deleteRecord", {
          params: {
            id: row.id,
            userType: 2,
          },
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$message.success(res.data.data);
            if (this.current > 1 && index == 0) {
              this.getGoodRecord(this.current - 1);
            } else {
              this.getGoodRecord(this.current);
            }
          } else {
            this.$message.error(res.data.data);
          }
          loading.close();
        })
        .catch((e) => {
          this.$message.error(res.data.data);
          loading.close();
        });
    },
  },
};
</script>

<style scoped>
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>